package com.lzq.badminton.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzq.badminton.context.BaseContext;
import com.lzq.badminton.dto.CourtTemplateDto;
import com.lzq.badminton.entity.Arena;
import com.lzq.badminton.entity.CourtTemplate;
import com.lzq.badminton.entity.UserAdmin;
import com.lzq.badminton.service.ArenaService;
import com.lzq.badminton.service.CourtTemplateService;
import com.lzq.badminton.service.UserAdminService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.CourtTemplateVo;
import com.lzq.badminton.vo.DetailCourtemplate;
import com.lzq.badminton.vo.HasCourtTemplateVo;
import com.lzq.badminton.vo.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courtTemplate")
public class CourtTemplateController {

    @Autowired
    private CourtTemplateService courtTemplateService;

    @Autowired
    private UserAdminService userAdminService;

    @Autowired
    private ArenaService arenaService;

    @PostMapping("/createCourtTemplate")
    @ApiOperation(value="创建场地时间安排表")
    public ResponseResult createCourtTemplate(@RequestBody List<CourtTemplateDto> courtTemplateDtoList){
        ResponseResult responseResult = courtTemplateService.addCourtTemplate(courtTemplateDtoList);
        return responseResult;
    }

    @GetMapping("/listCourtTemplate")
    @ApiOperation(value="按照球馆id展示场地时间安排表")
    public ResponseResult listCourtTemplate(){
        Long userId = BaseContext.getCurrentId();
        UserAdmin userAdmin = userAdminService.getById(userId);
        Long arenaId = userAdmin.getArenaId();
        List<CourtTemplateVo> courtTemplateVos = courtTemplateService.listCourtTemplate(arenaId);
        HasCourtTemplateVo hasCourtTemplateVo = new HasCourtTemplateVo();
        hasCourtTemplateVo.setCourtTemplateVoList(courtTemplateVos);
        hasCourtTemplateVo.setArenaId(arenaId);

        ResponseResult responseResult = ResponseResult.okResult(hasCourtTemplateVo);
        return responseResult;
    }

    @GetMapping("/deteilCourtTemplate")
    public ResponseResult deteilCourtTemplate(){
        Long currentId = BaseContext.getCurrentId();
        Long arenaId = userAdminService.getById(currentId).getArenaId();
        LambdaQueryWrapper<CourtTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourtTemplate::getArenaId, arenaId);
        List<CourtTemplate> courtTemplateList = courtTemplateService.list(lambdaQueryWrapper);
        List<CourtTemplateVo> courtTemplateVos = BeanCopyUtils.copyBeanList(courtTemplateList, CourtTemplateVo.class);
        Arena arena = arenaService.getById(arenaId);
        Integer courtSize = arena.getCourtSize();
        String timeSlots = arena.getTimeSlots();
        DetailCourtemplate detailCourtemplate = new DetailCourtemplate(courtSize, timeSlots, arenaId, courtTemplateVos);
        return ResponseResult.okResult(detailCourtemplate);

    }
}
